#!/usr/bin/perl -wT

# cord@debian.org

require 5.001;
use strict;
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
use Data::Dumper;
use List::Util 'shuffle';

require '/org/lists.debian.org/archive-spam-removals/etc/review.conf';
die ("$0: /org/lists.debian.org/archive-spam-removals/etc/review.conf incorrect: $!") unless $SpamReviewConf::max_reviews;

my $cachedir='/org/lists.debian.org/archive-spam-removals/review';
my $reviewlog='/org/lists.debian.org/spam-log/spam-review.log';
my $count_out = 10;
my @cache;

umask 002;

my $q = new CGI;

print $q->header;
my @names = $q->keywords;
my $remote_user = remote_user();
my %reviewed;
my %reviewcount;

open (HEADER, "/org/lists.debian.org/html/header.in") or 
  die ("$0: can't open /org/lists.debian.org/html/header.in: $!\n");
while (<HEADER>) {
  s/u_TITLE_u/Listarchive Review $names[0]/;
  print "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n" if m#^</head>$#;
  print "<script type=\"text/javascript\">
<!--
document.defaultAction = true;

// http://www.quirksmode.org/js/events_order.html
document.addEventListener('keydown', detectEvent, true);
if(document.captureEvents) document.captureEvents(Event.KEYDOWN);

function detectEvent(e)
{
  var evt = e || window.event;
  var pos = parseInt(window.location.hash.substring(1)) || $count_out+1;
  var elPrefix = '';

  switch(evt.keyCode) {
    case 74: // j
      pos -= 1;
      if (pos < 1) { pos = 1; }
      break;
    case 75: // k
      pos += 1;
      if (pos > $count_out) { pos = $count_out; }
      break;
    case 65: // a
      elPrefix = 'u';
      break;
    case 83: // s
      elPrefix = 'h';
      break;
    case 68: // d
      elPrefix = 'i';
      break;
    case 70: // f
      elPrefix = 's';
      break;
    case 90: // z
      fullPrefix = 'u';
      break;
    case 88: // x
      fullPrefix = 'h';
      break;
    case 67: // c
      fullPrefix = 'i';
      break;
    case 86: // v
      fullPrefix = 's';
      break;
  }

  if (1 <= pos && pos <= $count_out) {
    window.location.hash = pos;
  }

  if (elPrefix != '') {
    document.getElementById(elPrefix+pos).checked = true;
  }

  if (fullPrefix != '') {
    for (i = 0; i < 10; i++) {
      id = document.getElementById(fullPrefix+i);
      if (id) {
        id.checked = true;
      }
    }
  }

  return document.defaultAction;
}
// -->
</script></head>\n" if m#^</head>$#;
  print;
}
close (HEADER);

print "<H2>Listarchive review " . $names[0] . "</h2>\n";

die("$0: Wrong Call: $remote_user " . scalar(@names) . "\n") unless $remote_user and scalar(@names) == 1;

if (-e $reviewlog) {
  open (REVIEWLOG, $reviewlog) or die ("$0: can't open $reviewlog for reading: $!\n");
  while (<REVIEWLOG>) {
    if (m#^time: (\d+); addr: (\d+\.\d+\.\d+\.\d+); list: ([a-z0-9-]+), date: (\d+/\d*), msg: (msg\d+.html), rating: (Unsure|Spam|Ham|Inappropriate), reviewer: (\S+)$#) {
      $reviewed{"$3/$4/$5"}{$7} = $6;
      $reviewcount{"$3/$4/$5"}++;
    }
  }
  close (REVIEWLOG);
}

if (-e $cachedir . '/' . $names[0]) {
  open (CACHE, $cachedir . '/' . $names[0]) or die ("$0: can't open $cachedir/ " . $names[0] . " for reading: $!\n");

  print "<table border=\"1\">
	<tr><th>Rating<th>Description
	<tr><td>Ham<td>Ontopic Content
	<tr><td>Spam<td>Absolutely surely spam. For example some people take offense to some comments on the lists, but that is <strong>not</strong> spam.
	<tr><td>Inappropriate<td>misguided ((un)subscribe to list, test messages, replies to spam messages, vac messages, spam backscatter, probably NOT votes sent to debian-devel instead of devotee) messages, is is not entirely clear what to do with those, but please tag them accordingly.
	<tr><td>Unsure<td><strong>The Default</strong>. You are not sure if it is Spam or Ham or Inappropriate.</table>\n";

  print "
	<p><small>
		Keys: <b>j</b>/<b>k</b> move down/up, <b>a</b>/<b>s</b>/<b>d</b>/<b>f</b> mark current entry as Unsure/Ham/Inappropriate/Spam respectively, <b>z</b>/<b>x</b>/<b>c</b>/<b>v</b> mark all entries as Unsure/Ham/Inappropriate/Spam respectively
	</small></p>\n";

  print "<form action=\"/cgi-bin/review/review3.pl\" method=\"GET\">\n";

  foreach (<CACHE>) {
    chomp;
    push(@cache, $_);
  }

  foreach (shuffle(@cache)) {
    my ($post, $count, $msgid, $ham, $spam, $inappropriate) = split (/:/);
    my @reviewer = ( split(',', $ham), split(',', $spam), split(',', $inappropriate));
    next if grep (/^$remote_user$/, @reviewer);
    next if defined $reviewed{$post}{$remote_user};
    next if $reviewcount{$post} >= $SpamReviewConf::max_reviews;
    last unless $count_out--;
    print "<hr noshade width=\"100%\" size=\"1\"><a href=\"https://lists.debian.org/$post\" target=\"_blank\">https://lists.debian.org/$post</a>
      <a name=\"".($count_out+1)."\">&nbsp;</a> <!-- anchor for keyboard  -->
      <iframe src=\"https://lists.debian.org/$post\" name=\"https://lists.debian.org/$post\"
      width=\"95%\" height=\"400\" scrolling=\"yes\"
      hspace=\"10\" vspace=\"10\"
      marginwidth=\"20\" marginheight=\"10\" frameborder=\"1\">
      <a href=\"https://lists.debian.org/$post\" target=\"_blank\">https://lists.debian.org/$post</a>
      </iframe><br>\n";
    print "<label><input id=\"u$count_out\" type=\"radio\" name=\"$post\" value=\"Unsure\" checked> Unsure </label>\n";
    print "<label><input id=\"h$count_out\" type=\"radio\" name=\"$post\" value=\"Ham\"> Ham </label>\n";
    print "<label><input id=\"i$count_out\" type=\"radio\" name=\"$post\" value=\"Inappropriate\"> Inappropriate </label>\n";
    print "<label><input id=\"s$count_out\" type=\"radio\" name=\"$post\" value=\"Spam\"> Spam </label>\n";
  }
  close (CACHE);
} else {
  die "$0: can't find $cachedir/" . $names[0] . ": $!\n";
}

if ($count_out != 10) {
  print "<hr noshade width=\"100%\" size=\"1\">" . submit('Send') . reset('Cancel');
  print "<br>" . submit('Send & Continue');
} else {
  print "<hr noshade width=\"100%\" size=\"1\">Nothing to review left for $names[0]. You can continue here: <a href=\"/cgi-bin/review/review1.pl\">Debian mailarchive review</a>\n";
}

print "</form>\n";

print "<a href=\"/cgi-bin/review/review1.pl\">Debian mailarchive review</a>\n";

open (FOOTER, "/org/lists.debian.org/html/footer.in") or 
  die ("$0: can't open /org/lists.debian.org/html/footer.in: $!\n");
while (<FOOTER>) {
  print;
}
close (FOOTER);


exit 0;

